﻿//导入表定义
document.write("<script language='javascript' src='/resources/js/views/defineTable.js'></script>");

//导入表定义
var iHeight = document.documentElement.clientHeight-200;//判断表格自适应高度
var page = 1;
var pageSize = 200;

//本部门，监测指标的数据
var action = "/report/query/province.do";

//清除图形and表格数据
function clearData(){
	$("#view_containerzhuzi").html('');   //清空图形
	$("#view_containerzhuzi").height("0px");
	$("#view_containerzhuzi").width("0px");
	$('#tt').datagrid({data:[]});
}

function query(curPage, pageSize)  {

	curPage = parseInt(curPage);
	pageSize = parseInt(pageSize);

	$('#page').val(curPage);
	$('#pageSize').val(pageSize);

	var reportShowColumns = [
		defineTableReportAllColumns["iname"],
		defineTableReportAllColumns["code"],
		defineTableReportAllColumns["unit"],
		defineTableReportAllColumns["sname"]
	];

	var yeares=document.getElementsByName("year");
	var year="";
	var yearList= [];
	for(var i=0;i<yeares.length;i++){
		if(yeares[i].checked){
			yearList.push(yeares[i].value);
			year+=yeares[i].value+",";
		}
	}
	if(year!=""){
		year = year.substring(0, year.length-1);
	}

	for(var i=0; i< yearList.length; i++){
		var c = {title:yearList[i], field:yearList[i], width:90,align:"center",
			formatter: function (value, row, index) {
				// value 数据上报的类型_每年的目标值_数值_显示数据展示时效果的规则_每年的备注
				var tempValue;
				if(value.indexOf('_')!=-1){
					tempValue = value.split("_")[2];
				}
				if(tempValue===null||tempValue==""||tempValue=="null"||tempValue=="NULL"){
					return "";
				}else{
					return tempValue;
				}
			},
			styler: function (value, row, index) {
				/*
                     10  红色#ff0000  预期难以达标
                     20  橙色#FF9933  达标困难
                     30  黄色#ffff00  当年未达标经努力预期可达标
                     40  浅绿#039dea  达到当年标准
                     50  深绿#009966  提前达标
                 */
				var showlevel = 0;
				if(value.indexOf('_')!=-1){
					showlevel = value.split("_")[3];
				}

				switch (Number(showlevel)) {
					case 0 :
						return 'background-color:#FFFFFF;';
					case 10 :
						return 'background-color:#ff0000;';
					case 20 :
						return 'background-color:#FF9933;';
					case 30 :
						return 'background-color:#ffff00;';
					case 40 :
						return 'background-color:#039dea;';
					case 50 :
						return 'background-color:#009966;';
				}
				return 'background-color:#FFFFFF;';
			}
		};
		reportShowColumns.push(c);
	}
	reportShowColumns.push(defineTableReportAllColumns['aim']);//目标规划值
	reportShowColumns.push(defineTableReportAllColumns['rule']);//目标规则
	reportShowColumns.push(defineTableReportAllColumns['idesc']);//目标描述

	var iname = $('#iname').textbox('getValue');
	$.get(action, {"iname":iname, "years": year, "page": curPage, "pageSize": pageSize,"formsid":1},
		function (json) {

		if(json.code!=200) {
			clearData();
			return;
		}
		var data = json.data;
		var total = data["size"];
		if(total==""){
			$('#tt').datagrid({data:[]});
			return;
		}

		var totalPage = Math.ceil(total / pageSize);
		var start = (curPage - 1) * pageSize;
		var dataLength = 0;

		data = data["results"];
		if (data != null && data.length > 0) {
			dataLength = data.length;
		}

		$('#tt').datagrid( {
			height:iHeight,
			frozen:true,
			fitColumns: true,
			striped: true,  //显示条纹，隔行换色
			columns: [
				reportShowColumns
			],
			singleSelect: true,// 是否单选
			loadMsg: '数据加载中,请稍后......',
			pageNumber: curPage,//显示数据列用
			pageSize: pageSize,
			pageList: [200, 500, 1000],
			rownumbers: true,
			pagination: true,//打开翻页功能
			data: data
		});

		var p = $('#tt').datagrid('getPager');
		$(p).pagination({
			total: total,
			pageList: [200, 500, 1000],
			pageSize: pageSize,
			pageNumber: curPage,
			beforePageText: '第',
			afterPageText: '页    共 ' + totalPage + ' 页',
			displayMsg: '当前显示 ' + (start + 1) + '-' + (start + dataLength) + ' 条记录 共 ' + total + ' 条记录',
			onSelectPage: function (curPage, pageSize) {
				query(curPage, pageSize);
			},
			onChangePageSize: function (ps) {
				pageSize = ps;
			}
		});
		try {
			window.parent.unmask();
		} catch (err) {

		}
	}, "json");
}

var CRUD = {
	queryList: function () {
		query(page,pageSize);
	}
};

//导出Excel文件
function Save_Excel() {
	var columsFields=$("#tt").datagrid("getColumnFields");
	var CellStyles=[];

	var arrtable = [];
	var iTr=[];
	var iTable = document.getElementsByTagName("table");
	for (var j = 0; j < iTable.length; j++) {
		if (iTable[j].className == "datagrid-btable") {
			arrtable.push(iTable[j]);
		}
	}
	for (var i = 0; i < arrtable.length; i++) {
		if(arrtable[i].childNodes[0].childNodes.length>0){
			iTr = arrtable[i].getElementsByTagName("tr");
		}
	}

	for(var z=0; z<iTr.length;z++){
		var childNodess=iTr[z].childNodes;
		for (var i = 0; i < childNodess.length; i++) {
			var style={};
			var rgbS=childNodess[i].style.backgroundColor;
			var colorS='';
			if(rgbS==""){
				colorS='#ffffff';
			}else{
				colorS=rgb2hex(rgbS);
			}
			style['BgColor']=colorS;
			style['index']=z;
			style['field']=columsFields[i];
			CellStyles.push(style);
		}
	}

	var grid = $("#tt");
	var rows=grid.datagrid("getRows");					  //获得表格行数据
	var name='部门监测预警数据';
	var colums_all=grid.datagrid("options").columns;      		     //获得表格的所有列不包括冻结列
	var obj = $.ExportExcelDlg({
		HeadInfo: colums_all,    //表头栏
		RowInfo: rows,		   //数据航
		FooterInfo: grid.datagrid("getFooterRows"),
		RowStart: 2,		   //导出行位置
		ColumStart: 2,		   //导出列位置
		SheetName: name,
		CellStyles:CellStyles,
		MainTitle: { Displayname: name, Alignment: 'Center' },
		SaveName: name,
		Swf: 'ExportExcel.swf'
	});
	obj.ExportExcelDlg('open');
}

/*
 * rgb颜色转换
 */
function zero_fill_hex(num, digits) {
	var s = num.toString(16);
	while (s.length < digits)
		s = "0" + s;
	return s;
}

function rgb2hex(rgb) {

	if (rgb.charAt(0) == '#'){
		return rgb;
	}
	var ds = rgb.split(/\D+/);
	var decimal = Number(ds[1]) * 65536 + Number(ds[2]) * 256 + Number(ds[3]);
	return "#" + zero_fill_hex(decimal, 6);
}

$(function(){
	$("#tabDiv").height(iHeight+40);
	$(".panel-body").height('100%');
	$('#tablepanel').panel({
		title: '数据列表',
	});

	$('#tt').datagrid( {
		width : '100%',
		height : iHeight
	});

	$('#tabDiv').tabs({
		height:'700px',
		onSelect:function(title,index){

		}
	});
	$("#tabDiv").width($("#content").width())
	query(page, pageSize);
});
